package persistent;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.concurrent.BlockingQueue;







import connection.ConnectionManager;
import frame.Frame;

public class Persistent extends Thread {
	
	protected boolean active = true;
	
	
	
	private BlockingQueue<Frame> persistentQueue;
	private static ConnectionManager manager = null;
	
	public Persistent(BlockingQueue<Frame> persistentQueue){
		
		this.persistentQueue = persistentQueue;
		manager = new ConnectionManager();
		
	}
	
	@Override
	public void run(){
		PreparedStatement psInsertar = null;
		PreparedStatement psPlaca = null;
		PreparedStatement psActualizar = null;
		while(active){
			
			try {
				Thread.sleep(500);
				
				
				manager.open();
				for(int i=0; i<persistentQueue.size(); i++){
					
					String placasVehiculo="";
					int kilometraje=40;//no hay de donde obtenerlo
					
					psPlaca = manager.getConexion().prepareStatement("SELECT placas FROM vehiculo WHERE codigo_vehiculo=?");
					psPlaca.setString(1,persistentQueue.take().getCodVehiculo());
					
					ResultSet rs = psPlaca.executeQuery();
					
					while (rs.next())
					{
						placasVehiculo =rs.getString(1);
						break;
					}
					
					psActualizar = manager.getConexion().prepareStatement("UPDATE trama SET edad_dato=0 WHERE codigo_vehiculo=? AND placas_vehiculo=?");
					psActualizar.setString(1,persistentQueue.take().getCodVehiculo());
					psActualizar.setString(2,placasVehiculo);
					psActualizar.executeUpdate();
					
					psInsertar = manager.getConexion().prepareStatement("INSERT INTO trama ( ano,mes,dia,segundo,longitud,latitud,fuente,velocidad,edad_dato,"+ 
							"sensor_temperatura_activo,sensor_temperatura_valor,choque,puerta,"+ 
							"ignicion,kilometraje,codigo_vehiculo,codigo_evento,actual,"+ 
							"placas_vehiculo,hora,minuto) VALUES"+
							"(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
					
					SimpleDateFormat year = new SimpleDateFormat("yyyy");
					SimpleDateFormat month = new SimpleDateFormat("MM");
					SimpleDateFormat day = new SimpleDateFormat("dd");
					SimpleDateFormat hour = new SimpleDateFormat("HH");
					SimpleDateFormat minute = new SimpleDateFormat("mm");
					SimpleDateFormat seconds = new SimpleDateFormat("ss");
					
					psInsertar.setInt(1,Integer.parseInt(year.format(persistentQueue.take().getFecha())));//ano  
					psInsertar.setInt(2,Integer.parseInt(month.format(persistentQueue.take().getFecha())));//mes  
					psInsertar.setInt(3,Integer.parseInt(day.format(persistentQueue.take().getFecha())));//dia  
					psInsertar.setInt(4,Integer.parseInt(seconds.format(persistentQueue.take().getFecha())));//segundo  
					psInsertar.setDouble(5,persistentQueue.take().getLongitud());
					psInsertar.setDouble(6,persistentQueue.take().getLatitud());
					psInsertar.setBoolean(7,persistentQueue.take().getFuente()==1 ? true : false);
					psInsertar.setInt(8,persistentQueue.take().getVelocidad());
					psInsertar.setBoolean(9,persistentQueue.take().getEdadDato()==1 ? true : false);
					psInsertar.setBoolean(10,persistentQueue.take().getTempAct()==1 ? true : false);
					psInsertar.setInt(11,persistentQueue.take().getTempVal());
					psInsertar.setInt(12,persistentQueue.take().getChoque());
					psInsertar.setString(13,persistentQueue.take().getPuertas());
					psInsertar.setInt(14,persistentQueue.take().getIgnicion());
					psInsertar.setInt(15,kilometraje);//kilometraje
					psInsertar.setString(16,persistentQueue.take().getCodVehiculo());
					psInsertar.setInt(17,persistentQueue.take().getCodEvento());
					psInsertar.setBoolean(18,true);//actual
					psInsertar.setString(19,placasVehiculo);//placas_vehiculo
					psInsertar.setInt(20,Integer.parseInt(hour.format(persistentQueue.take().getFecha())));//hora  
					psInsertar.setInt(21,Integer.parseInt(minute.format(persistentQueue.take().getFecha())));//minuto  
					psInsertar.executeUpdate();
					System.out.println(persistentQueue.take().toString());
				}
				
				
				manager.close();
			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
